package com.coollang.smartbra.fragment;

import android.support.annotation.IdRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.activity.Welcome2Activity;
import com.coollang.smartbra.base.BaseFragment;
import com.coollang.smartbra.constant.Constants;
import com.coollang.smartbra.dialog.SetWeelDialog;
import com.coollang.smartbra.eventbus.CommonEvent;
import com.coollang.smartbra.utils.UIUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.greenrobot.event.EventBus;

/**
 * @author : Timmy
 * @data : 2017/7/20
 * @Fuction :
 */
public class SetUserInformationFragment extends BaseFragment {

    Unbinder unbinder;
    @BindView(R.id.sex_radio)
    RadioGroup mSexRadio;
    @BindView(R.id.tv_date)
    TextView mTvDate;
    @BindView(R.id.tv_height)
    TextView mTvHeight;
    @BindView(R.id.tv_weight)
    TextView mTvWeight;
    @BindView(R.id.tv_finish)
    TextView mTvFinish;

    // 年龄的取值
    private int mSex = 0;
    private Map<String, String> mMap = new HashMap<>();

    @Override
    protected View inflateView(LayoutInflater inflater) {
        View inflate = inflater.inflate(R.layout.item_set_user_infomation, null, false);
        unbinder = ButterKnife.bind(this, inflate);
        return inflate;
    }

    @Override
    protected void initView(View view) {
        mSexRadio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId == R.id.rb_male) {
                    mSex = 1;
                } else if (checkedId == R.id.rb_female) {
                    mSex = 0;
                }
                addPersonInfo("gender", mSex + "");
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initData() {
        addPersonInfo("gender", mSex + "");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.tv_date, R.id.tv_height, R.id.tv_weight, R.id.tv_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_date:
                new SetWeelDialog(getActivity(), Constants.TYPE_SETDATE, "1998", "12", "22").show();
                break;
            case R.id.tv_height:
                new SetWeelDialog(getActivity(), Constants.TYPE_SETHEIGHT, "1998", "12", "22").show();
                break;
            case R.id.tv_weight:
                new SetWeelDialog(getActivity(), Constants.TYPE_SETWEIGHT, "1998", "12", "22").show();
                break;
            case R.id.tv_finish:

                ((Welcome2Activity) getActivity()).requestChangeUserInfo(mMap);
                break;
        }
    }

    public void onEventMainThread(CommonEvent event) {
        switch (event.type) {
            case CommonEvent.SETWEIGHT:
                mTvWeight.setText(event.msg + UIUtils.getString(R.string.unit_kg));
                addPersonInfo("weight", event.msg);
                break;
            case CommonEvent.SETHEIGHT:
                mTvHeight.setText(event.msg + UIUtils.getString(R.string.unit_cm));
                addPersonInfo("height", event.msg);
                break;
            case CommonEvent.SETDATE:
                mTvDate.setText(event.msg);
                addPersonInfo("birth", event.msg);
                break;
        }
        setBtnFinish();
    }

    private void setBtnFinish() {
        if (!mTvDate.getText().toString().contains("-")) {
        } else if (!mTvHeight.getText().toString().contains("cm")) {
        } else if (!mTvWeight.getText().toString().contains("kg")) {
        } else {
            // 显示完成按钮
            mTvFinish.setVisibility(View.VISIBLE);
        }
    }

    private void addPersonInfo(String key, String value) {
        mMap.put(key, value);
    }
}
